<?php

namespace app\admin\controller;

use think\Controller;

class Member extends Controller
{
    //会员列表
    public function all(){
        $members = model('Member')->order('create_time', 'desc')->paginate('10');
        $this->assign('members', $members);
        return view();
    }

    //会员添加
    public function add(){
        if (request()->isAjax()){
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password'),
                'conpass' => input('post.conpass'),
                'nickname' => input('post.nickname'),
                'email' => input('post.email')
            ];
            $result = model('Member')->add($data);
            if ($result == 1){
                $this->success('会员添加成功！', 'admin/member/all');
            }else{
                $this->error($result);
            }
        }
        return view();
    }

    //会员编辑
    public function edit(){
        $id = input('id');
        if (request()->isAjax()){
            $data = [
                'id' => $id,
                'oldpass' => input('post.oldpass'),
                'newpass' => input('post.newpass'),
                'newconpass' => input('post.conpass'),
                'nickname' => input('post.nickname')
            ];
            $result = model('Member')->edit($data);
            if ($result == 1){
                $this->success('编辑成功', 'admin/member/all');
            }else{
                $this->error($result);
            }
        }
        $memberInfo = model('Member')->find($id);
        $this->assign('memberInfo', $memberInfo);
        return view();
    }

    //会员删除
    public function del(){
        $memberInfo = model('Member')->find(input('post.id'));
        $memberInfo->delete();
        if ($memberInfo){
            $this->success('删除成功！','admin/member/all');
        }else{
            $this->error('删除失败！');
        }
    }

    public  function ed(){
        return view();
    }

}
